<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>doh.robot InlineEditBox Test</title>

		<style>
			@import "../../../util/doh/robot/robot.css";
		</style>

		<!-- required: dojo.js -->
		<script type="text/javascript" src="../../../dojo/dojo.js"
			djConfig="isDebug: true"></script>

		<!-- functions to help test -->
		<script type="text/javascript" src="../helpers.js"></script>

		<script type="text/javascript">
			dojo.require("dojo.date.locale");
			dojo.require("dijit.robotx");

			dojo.addOnLoad(function(){
				doh.robot.initRobot('../test_InlineEditBox.html');

				function moveAndClick(node) {
					doh.robot.mouseMoveAt(node, 500, 500);
					doh.robot.mouseClick({left: true}, 500);
				}
				function cancel(deferred, widget){
					moveAndClick(function(){
						try{
							var buttons = dojo.query(".cancelButton", widget.domNode.parentNode);
							return buttons[buttons.length-1];
						}catch(e){ deferred.errback(e); }
					});
					doh.robot.sequence(function(){}, 1000); // slow down to allow widget to exit edit mode
				}
				function save(deferred, widget){
					moveAndClick(function(){
						try{
							var buttons = dojo.query(".saveButton", widget.domNode.parentNode);
							return buttons[buttons.length-1];
						}catch(e){ deferred.errback(e); }
					});
					doh.robot.sequence(function(){}, 1000); // slow down to allow widget to exit edit mode
				}
				function testOK(deferred, delay){
					doh.robot.sequence(dojo.hitch(deferred, "callback", true), delay? delay : 1);
				}

				doh.register("dijit.InlineEditBox autosave tests", [
					{
						name: "CurrencyTextBox invalid value: blur",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar_as");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("dollar", 1000, 1200); // invalid
							moveAndClick("predefined");
							doh.robot.sequence(d.getTestCallback(function(){
								var currencyTextBox = inlineBox.wrapperWidget.editWidget;
								doh.is('dollar', currencyTextBox.get("displayedValue"), "displayedValue");
								doh.f(currencyTextBox.isValid(), "!isValid");
								inlineBox.cancel(false);
								doh.is('', inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "CurrencyTextBox invalid value: ENTER",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar_as");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("dollar", 1000, 1200); // invalid
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});
							doh.robot.sequence(d.getTestErrback(function(){
								var currencyTextBox = inlineBox.wrapperWidget.editWidget;
								doh.is('dollar', currencyTextBox.get("displayedValue"), "displayedValue");
								doh.f(currencyTextBox.isValid(), "!isValid");
							}), 1000);
							doh.robot.keyPress(dojo.keys.ESCAPE, 500, {});
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is('', inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "CurrencyTextBox valid value: blur",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar_as");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("123", 1000, 600);
							moveAndClick("predefined");
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is("$123.00", inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "CurrencyTextBox valid value: ENTER",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar_as");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("234", 1000, 600);
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is('$234.00', inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "CurrencyTextBox valid value: ESCAPE",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar_as");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("345", 1000, 600);
							doh.robot.keyPress(dojo.keys.ESCAPE, 500, {});
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is("$234.00", inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "CurrencyTextBox valid value: TAB",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar_as");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("456", 1000, 600);
							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is('$456.00', inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "Focus test",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("editable");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("editable", 1000, 1600);
							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.keyPress(dojo.keys.TAB, 1500, {}); // wait more than 1 second to TAB
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});
							doh.robot.typeKeys("567", 500, 600);
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});
							doh.robot.keyPress(dojo.keys.TAB, 500, {shift:true}); // do this fast
							doh.robot.keyPress(dojo.keys.TAB, 500, {shift:true}); // do this fast
							doh.robot.keyPress(dojo.keys.ENTER, 500, {}); // quickly back to editable
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500, {});
							doh.robot.typeKeys(" again", 1000, 1200);
							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is('$567.00', dijit.byId("dollar_as").get("value"), "value");
								doh.is('editable again', inlineBox.get("value"), "value");
							}), 1000);
							return d;
						}
					},
					{
						name: "Textarea test",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("textarea");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("line 1", 1000, 1200);
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});
							doh.robot.typeKeys("line 2", 500, 1200);
							moveAndClick("predefined");
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is('line 1\\nline 2', inlineBox.get("value").replace(/\n/g, "\\n"), "textarea programmatic value was " + inlineBox.get("value").replace(/\n/g, "\\n"));
								doh.is('line 1<br>line 2', inlineBox.domNode.innerHTML.toLowerCase(), "textarea rendered value was " + inlineBox.domNode.innerHTML);
							}), 1000);
							return d;
						}
					}
				]);

				// Testing that the initial display value of the InlineEditBox gets sent to the editor.
				// For example, in this markup:
				//		<span dojoType="dijit.InlineEditBox" ...>01/05/2007</span>
				// When the editor is clicked the calendar should open to that date.
				doh.register("dijit.InlineEditBox preDefinedValue tests", [
					{
						name: "CurrencyTextBox preDefinedValue",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("dollar");
							moveAndClick(inlineBox.domNode);
							doh.robot.sequence(d.getTestErrback(function(){
								doh.is('$2,000', inlineBox.get("value"), "value");
								var currencyTextBox = inlineBox.wrapperWidget.editWidget;
								doh.is('2000.00', currencyTextBox.get("displayedValue"), "displayedValue");
							}), 1000);
							cancel(d, inlineBox);
							testOK(d);
							return d;
						}
					},
					{
						name: "NumberSpinner preDefinedValue",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("quantity");
							moveAndClick(inlineBox.domNode);
							doh.robot.sequence(d.getTestErrback(function(){
								doh.is(3, inlineBox.get("value"));
								var numberSpinner = inlineBox.wrapperWidget.editWidget;
								doh.is(3, numberSpinner.get("value"));
							}), 1000);
							cancel(d, inlineBox);
							testOK(d);
							return d;
						}
					},
					{
						name: "ComboBox preDefinedValue",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("item");
							moveAndClick(inlineBox.domNode);
							doh.robot.sequence(d.getTestErrback(function(){
								doh.is("refrigerators", dojo.trim(inlineBox.get("value")));
								var comboBox = inlineBox.wrapperWidget.editWidget;
								doh.is("refrigerators", comboBox.get("value"));
							}), 1000);
							cancel(d, inlineBox);
							testOK(d);
							return d;
						}
					},
					{
						name: "DateTextBox preDefinedValue",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("purchase");
							moveAndClick(inlineBox.domNode);
							var preDate = dojo.date.locale.parse("01/05/2007", {datePattern: 'MM/dd/yyyy', selector:'date'});
							doh.robot.sequence(d.getTestErrback(function(){
								doh.is("01/05/2007", inlineBox.get("value"));
								var dateTextBox = inlineBox.wrapperWidget.editWidget;
								doh.is(preDate.toString(), dateTextBox.get("value").toString());
							}), 1000);
							cancel(d, inlineBox);
							testOK(d);
							return d;
						}
					},
					{
						name: "FilteringSelect preDefinedValue",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("state");
							moveAndClick(inlineBox.domNode);
							doh.robot.sequence(d.getTestErrback(function(){
								doh.is("Pennsylvania", dojo.trim(inlineBox.get("value")));
								var filteringSelect = inlineBox.wrapperWidget.editWidget;
								doh.is("PA", filteringSelect.get("value"));
							}), 1000);
							cancel(d, inlineBox);
							testOK(d);
							return d;
						}
					}
				]);

				doh.register("renderAsHtml", [

					// "plain text" mode, < > & have no special meaning
					{
						name: "false",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();

							// confirm initial displayed value
							var inlineBox = dijit.byId("renderAsHtml_false");
							doh.is("&lt;B&gt;not bold&lt;/B&gt;&amp;lt;input&amp;gt;", dojo.trim(inlineBox.domNode.innerHTML),
								"confirm initial value (with special chars) wasn't modified");
							doh.is("<B>not bold</B>&lt;input&gt;", inlineBox.get("value"),
								"confirm initial get('value')");

							// launch editor, and make sure that special characters aren't mangled
							moveAndClick(inlineBox.domNode);
							doh.robot.sequence(d.getTestErrback(function(){
								var editor = inlineBox.wrapperWidget.editWidget;
								doh.is("<B>not bold</B>&lt;input&gt;", editor.get("value"), "initial editor value");
							}), 1000);

							// edit value and save		
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 1000, {});
							doh.robot.typeKeys("end", 2000, 1000);
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							// check that display value didn't mangle special characters
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is("&lt;B&gt;not bold&lt;/B&gt;&amp;lt;input&amp;gt;end", dojo.trim(inlineBox.domNode.innerHTML),
									"displayed value after edit");
								doh.is("<B>not bold</B>&lt;input&gt;end", inlineBox.get("value"),
									"get('value') after edit");
							}), 1000);

							return d;
						}	
					},

					// HTML mode, display mode has rich text, which is passed to editor
					{
						name: "true",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();

							// confirm initial displayed value
							var inlineBox = dijit.byId("renderAsHtml_true");
							doh.is("&lt;b&gt;not bold&lt;/b&gt;<b>bold</b>&amp;lt;input&amp;gt;",
								dojo.trim(inlineBox.domNode.innerHTML).toLowerCase(),
								"confirm initial value (with special chars) wasn't modified");
							doh.is("&lt;b&gt;not bold&lt;/b&gt;<b>bold</b>&amp;lt;input&amp;gt;",
								inlineBox.get("value").toLowerCase(),
								"confirm initial get('value')");

							// launch editor, and make sure that value was passed to editor as HTML
							moveAndClick(inlineBox.domNode);
							doh.robot.sequence(d.getTestErrback(function(){
								var editor = inlineBox.wrapperWidget.editWidget;
								doh.is("&lt;b&gt;not bold&lt;/b&gt;<b>bold</b>&amp;lt;input&amp;gt;",
									editor.get("value").toLowerCase(),
									"initial editor value");
							}), 1000);

							// edit value and save		
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 1000, {});
							doh.robot.typeKeys("end", 2000, 1000);
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							// check that display value didn't mangle special characters
							doh.robot.sequence(d.getTestCallback(function(){
								doh.is("&lt;b&gt;not bold&lt;/b&gt;<b>bold</b>&amp;lt;input&amp;gt;end",
									dojo.trim(inlineBox.domNode.innerHTML).toLowerCase(),
									"displayed value after edit");
								doh.is("&lt;b&gt;not bold&lt;/b&gt;<b>bold</b>&amp;lt;input&amp;gt;end",
									inlineBox.get("value").toLowerCase(),
									"get('value') after edit");
							}), 1000);

							return d;
						}	
					}
				]);


				doh.register("focus tests", [
					{
						name: "RTE",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred();
							var inlineBox = dijit.byId("inlineRTE");
							moveAndClick(inlineBox.domNode);
							doh.robot.typeKeys("!start!", 1000, 1400);
							save(d, inlineBox);
							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(inlineBox.get("value").indexOf('!start!') >= 0, "value changed");
							}), 1000);
							return d;
						}
					}
				]);

				doh.run();
			});
		</script>
	</head>
</html>
